package com.rowling.factory;

import lombok.Getter;

import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.MirroredTypeException;

/**
 * 注解类（ 把注解翻译成的注解类）
 */
@Getter
public class FactoryAnnotatedClass {

    private TypeElement typeElement;//获取被@Factory注解的原始元素
    private String qualifiedFactoryGroupName;//获取在 Factory#type()指定的类型合法全名
    private String simpleFactoryGroupName;//获取在 Factory#type() 中指定的类型的简单名字
    private String id;   //获取在Factory#id()中指定的id

    /**
     * 注意  aClass是一个真正的Class对象
     * @param classElement
     */
    public FactoryAnnotatedClass(TypeElement classElement){
        try{
            //加了@Factory的代码已经编译过了
            Factory annotation = classElement.getAnnotation(Factory.class);
            id = annotation.id();
            Class aClass = annotation.getClass();
            qualifiedFactoryGroupName = aClass.getCanonicalName();
            simpleFactoryGroupName = aClass.getSimpleName();
            System.out.println("try123qualifiedFactoryGroupName:="+qualifiedFactoryGroupName+",simpleFactoryGroupName="+simpleFactoryGroupName);
        }catch (MirroredTypeException e){
            // 没有编译过，会抛出编译异常，在异常里面，拿到它的注解的id,注解的class,只能通过异常去获取
            DeclaredType classTypeMirror = (DeclaredType) e.getTypeMirror();
            TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
            qualifiedFactoryGroupName = classTypeElement.getQualifiedName().toString();
            simpleFactoryGroupName = classTypeElement.getSimpleName().toString();
            System.out.println("exception123qualifiedFactoryGroupName:="+qualifiedFactoryGroupName+",simpleFactoryGroupName="+simpleFactoryGroupName);
        }


    }


}
